package com.seg.web.controller.wms;

import com.seg.common.annotation.Log;
import com.seg.common.core.controller.BaseController;
import com.seg.common.core.domain.AjaxResult;
import com.seg.common.core.domain.entity.SysUser;
import com.seg.common.core.page.TableDataInfo;
import com.seg.common.enums.BusinessType;
import com.seg.common.utils.poi.ExcelUtil;
import com.seg.system.domain.TOutWarehouse;
import com.seg.system.domain.TSparePart;
import com.seg.system.service.ISysUserService;
import com.seg.system.service.ITOutWarehouseService;
import com.seg.system.service.ITSparePartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-06-29
 */
@RestController
@RequestMapping("/wms/warehouse")
public class TOutWarehouseController extends BaseController {
    @Autowired
    private ITOutWarehouseService tOutWarehouseService;

    @Autowired
    private ITSparePartService itSparePartService;

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:out')")
    @GetMapping("/list")
    public TableDataInfo list(TOutWarehouse tOutWarehouse) {
        startPage();
        List<TOutWarehouse> list = tOutWarehouseService.selectTOutWarehouseList(tOutWarehouse);
        for(int i=0;i<list.size();i++){
            TSparePart tSparePart = new TSparePart();
            tSparePart.setMaterialNo(list.get(i).getMaterialNo());
            List<TSparePart> tSparePartList = itSparePartService.selectTSparePartList(tSparePart);
            list.get(i).settSparePart(tSparePartList.get(0));

            SysUser sysUser = iSysUserService.selectUserByUserName(list.get(i).getCreateBy());
            list.get(i).setSysUser(sysUser);
        }
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:out:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOutWarehouse tOutWarehouse) {
        List<TOutWarehouse> list = tOutWarehouseService.selectTOutWarehouseList(tOutWarehouse);
        ExcelUtil<TOutWarehouse> util = new ExcelUtil<>(TOutWarehouse.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tOutWarehouseService.selectTOutWarehouseById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:add')")
    @Log(title = "【备件出库】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOutWarehouse tOutWarehouse) {
        if(tOutWarehouse.getOutNum() <= 0L){
            return toAjax1(-1, "出库数量必须大于0");
        }
        String operName = getUsername();
        int flag = tOutWarehouseService.insertTOutWarehouse(tOutWarehouse, operName);
        if (flag == -1) {
            return toAjax1(flag, "备件不存在");
        } else if (flag == -2) {
            return toAjax1(flag, "库存不足");
        }
        return toAjax1(flag, "出库成功");
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOutWarehouse tOutWarehouse) {
        return toAjax(tOutWarehouseService.updateTOutWarehouse(tOutWarehouse));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:out:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tOutWarehouseService.deleteTOutWarehouseByIds(ids));
    }

    @PostMapping("/get_status")
    public TableDataInfo getStatus() {
        return getDataTable(tOutWarehouseService.getStatus());
    }

}
